<template>
	<view>
		<u-sticky bgColor="#fff">
			<u-tabs :list="tab" lineColor="#2f9fff" :activeStyle="{color:'#2f9fff'}" :is-scroll="false" :scrollable="false"
				@change="_change"></u-tabs>
		</u-sticky>
		<view class="record_box" v-if="data !== undefined && data!== null && data .length>0">
			<view class="record_list" v-for="(item,index) in data" :key="index">
				<view class="record_tit" :style="bg_color.success.tit" v-if="item.confirm_type == 1">
					<text v-if="item.type == 1" style="color: #2f9fff;">已接收【调拨】</text>
					<text v-if="item.type == 2" style="color: #2f9fff;">已接收【回退】</text>
					<text style="background-color: #2f9fff;padding: 0rpx 20rpx;border-radius: 16rpx; color: #fff;"
						@click="sn_open(item.sn)">SN</text>
				</view>
				<view class="record_tit" :style="bg_color.fail.tit" v-else-if="item.confirm_type == 2">
					<text v-if="item.type == 1" style="color: #ff8229;">已拒绝【调拨】</text>
					<text v-if="item.type == 2" style="color: #ff8229;">已拒绝【回退】</text>
					<text style="background-color: #ff8229;padding: 0rpx 20rpx;border-radius: 16rpx;color: #fff;"
						@click="sn_open(item.sn)">SN</text>
				</view>
				<view class="record_tit" :style="bg_color.success.tit" v-else>
					<text style="color: #07c160;">调拨中</text>
					<text style="background-color: #ebfaf2;padding: 0rpx 20rpx;border-radius: 16rpx;color: #07c160;"
						@click="sn_open(item.sn)">SN</text>
				</view>
				<view class="record_cont" :style="item.confirm_type == 2?bg_color.fail.cont:bg_color.success.cont">
					<h2>{{item.number}}<text>台</text></h2>
					<text v-if="item.product != null">产品：{{item.product.name}}</text>
					<text>服务商名称：{{item.from_info.name}}-[{{item.from_info.type}}]</text>
					<text v-if="type == 2">调拨去向：{{item.nickname}}</text>
					<block v-if="type ==1">
						<view class="select_box" v-if="item.confirm_type == 0">
							<text @click="receive_prompt(item.id,item.type)">接收</text>
							<text @click="show = true">拒绝</text>
						</view>
						<u-popup :show="show" @close="close" mode="center" :round="10"
							:overlayStyle="{background: 'rgba(0, 0, 0, 0.2)'}">
							<view class="pop_box">
								<h5 class="pop_box_tit">输入内容</h5>
								<input type="text" placeholder="输入拒绝理由" v-model="remark">
								<view class="" @click="refuse(item.id)">确定</view>
							</view>
						</u-popup>
					</block>
				</view>
			</view>
		</view>
		<u-empty v-else mode="list" icon="/static/Empty/msglist_null.png" text="暂无信息">
		</u-empty>
		<!-- refuse(item.id) -->
		<u-popup :show="SN_show" mode="center" :round="10" @close="SN_close">
			<view class="mask">
				<view class="content" v-if="SnList.length > 0">
					<view class="explain_box" v-for="(item,index) in SnList" :key="index">
						<text>{{index+1}}. {{item}}</text>
						<text class="copy" @click="_copy(item)">复制</text>
					</view>
				</view>
				<view class="content" v-else>
					<u-empty mode="list" icon="/static/Empty/msg_null.png" text="暂无终端">
					</u-empty>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bg_color: {
					success: {
						tit: 'background-image: linear-gradient(90deg,#f0f7ff, #fafcff);',
						cont: 'background-image: linear-gradient(#fff, #fff);'
					},
					fail: {
						tit: 'background-image: linear-gradient(90deg,#fff1df, #fffbf7);',
						cont: 'background-image: linear-gradient(#fff, #fff);'
					}
				},
				tab: [{
						name: '调拨中',
						type: 0
					}, {
						name: '已接收',
						type: 1
					},
					{
						name: '已拒绝',
						type: 2
					}
				],
				data: [],
				id: null,
				type: null,
				remark: '',
				show: false,
				bottom: true,
				page: 1,
				SN_show: false,
				SnList: [],
				screen_nmb: 0,
				screen_boor: false
			}
		},
		onLoad(e) {
			uni.setNavigationBarTitle({
				title: e.tit
			})
			this.id = e.id
			this.type = e.type
			this._other()
		},
		//下拉刷新
		onReachBottom() {
			if (this.bottom == true) {
				this.page += 1
				this._other()
			}
		},
		methods: {
			_change(e) {
				this.screen_nmb = e.type
				this.page = 1
				this.bottom = true
				this._other()
			},
			SN_close() {
				this.SN_show = false
			},
			sn_open(sn) {
				this.SN_show = true
				this.SnList = sn
			},
			_copy(sn) {
				uni.setClipboardData({
					data: sn,
					success: function() {}
				});
			},
			//调用接口
			async _other() {
				uni.showLoading({
					title: '加载中'
				});
				const userinfo = uni.getStorageSync('userinfo');
				const {
					data: res
				} = await this.$myHttp5({
					url: 'setting/getAllocationList',
					data: {
						type: this.type, //调拨列表类型，1=接收方，2=设置方
						page_size: 15,
						page: this.page,
						confirm_type: this.screen_nmb,
						product_id: this.id
					},
					header: {
						'token': userinfo.token
					},
				});
				if (res.code == 1) {
					if (res.data.data !== undefined && res.data.data !== null && res.data.data.length >
						0) {
						if (this.page == 1) {
							this.data = res.data.data
							uni.hideLoading()
						} else {
							this.data = [...this.data, ...res.data.data]
							uni.hideLoading()
						}
					} else {
						if (this.page == 1) {
							this.data = res.data.data
							uni.hideLoading()
						} else {
							uni.hideLoading()
							this.bottom = false
						}
					}
				} else {
					uni.showToast({
						title: res.msg,
						duration: 1000,
						icon: "none"
					});
					this.bottom = false
				}
			},
			//接受
			receive_prompt(id, type) {
				let _this = this
				let _url = ''
				if (type == 1) {
					_url = 'setting/acceptAllocation'
				} else if (type == 2) {
					_url = 'setting/acceptRollback'
				}
				uni.showModal({
					title: '提示',
					content: '是否接收',
					success: function(res) {
						if (res.confirm) {
							_this.receive(id, _url)
						}
					}
				});

			},
			async receive(id, url) {
				uni.showLoading({
					title: '加载中'
				});
				let _this = this
				const userinfo = uni.getStorageSync('userinfo');
				const {
					data: res
				} = await this.$myHttp5({
					url: url,
					data: {
						id: id
					},
					header: {
						'token': userinfo.token
					},
				});
				if (res.code == 0) {
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: res.msg,
						duration: 2000,
						icon: 'none',
						success() {
							setTimeout(() => {
								_this._other()
							}, 2000)
						}
					})
				}

			},
			//拒绝
			close() {
				this.show = false
			},
			async refuse(id) {
				uni.showLoading({
					title: '加载中'
				});
				const userinfo = uni.getStorageSync('userinfo');
				const {
					data: res
				} = await this.$myHttp5({
					url: 'setting/denyAllocation',
					data: {
						id: id,
						remark: this.remark
					},
					header: {
						'token': userinfo.token
					},
				});
				let _this = this
				uni.showToast({
					title: res.msg,
					duration: 2000,
					icon: 'none',
					success() {
						_this.$router.go(0)
					}
				})
				this.show = true

			}
		}
	}
</script>

<style lang="scss" scoped>
	.select_box {
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-top: #f7f7f7 1px solid;
		margin-top: 20rpx;

		text {
			color: #333;
			text-align: center;
			display: block;
			width: 50%;
		}
	}

	.record_box {
		box-sizing: border-box;

		border-radius: 7px;
		width: 90%;
		margin: 15px auto;

		box-shadow: 7px 0px 30px -18px #888888;
		padding-bottom: 10px;


		.record_list {
			border-radius: 15rpx;
			overflow: hidden;
			margin-bottom: 30rpx;

			.record_tit {
				display: flex;
				justify-content: space-between;
				padding: 20rpx;
				border-bottom: #eee solid 1px;

				text {
					font-size: 28rpx;
					color: #2f9fff;
				}
			}

			.record_cont {
				padding: 20rpx;
				display: flex;
				flex-direction: column;

				h2 {
					font-weight: 500;
					margin-bottom: 10rpx;
					color: #ff7307;

					text {
						font-size: 28rpx;
						padding-left: 10rpx;
					}
				}

				text {
					font-size: 24rpx;
					margin-top: 20rpx;
				}
			}
		}
	}

	.pop_box {
		background-color: #fff;
		padding: 30rpx 50rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		border-radius: 15rpx;
		box-sizing: border-box;

		.pop_box_tit {
			font-size: 28px;
			color: #8d8d8d;
			display: block;
			margin-bottom: 30rpx;
			font-weight: 500;
		}

		input {
			font-size: 28rpx;
			border: #eee 1px solid;
			padding: 10rpx;
		}

		view {
			width: 100%;
			background-color: #1e80ff;
			color: #fff;
			margin-top: 40rpx;
			text-align: center;
			padding: 20rpx;
			box-sizing: border-box;
			border-radius: 15rpx;
		}
	}


	.mask {
		width: 100%;
		height: 100%;
		padding: 30rpx;
		box-sizing: border-box;
	}

	.content {
		display: flex;
		flex-direction: column;
		// justify-content: space-between;
		// align-content: space-between;
		line-height: 55rpx;
		height: 40vh;
		width: 70vw;
		overflow-y: auto;

		.explain_box {
			font-size: 26rpx;
			display: flex;
			justify-content: space-between;
			box-sizing: border-box;
			margin-top: 20rpx;

			.copy {
				background-image: linear-gradient(to right, #0dadf7, #0ee5f8);
				color: #fff;
				padding: 0 20rpx;
				border-radius: 15rpx;
				display: block;
				box-sizing: border-box;
			}
		}
	}
</style>